package com.cn.kehong.utils;

import net.sf.jxls.transformer.XLSTransformer;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.Map;

/**
 * @类名:TemplateExcelUtils
 * @包名:com.cn.kehong.utils
 * @作者:chenxingliang
 * @时间:2022-12-09 09:37
 * @版本:1.0.0
 */
public class TemplateExcelUtils {


    /**
     * 根据模板导出数据
     * @param beanParams
     * @param response
     * @throws Exception
     */
    public static void downLoadExcel(String filePath, String outFileName, Map<String, Object> beanParams, String templateFile, HttpServletResponse response)
            throws Exception {
        OutputStream os = null;
        InputStream is = null;
        try{
            response.reset();
            // 设置response的Header
            response.setCharacterEncoding("utf-8");
            response.setContentType("multipart/form-data");
            response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(outFileName, "utf-8"));
            //读取模板
            String xlsTemplateFileName = TemplateExcelUtils.class.getClassLoader().getResource(templateFile).getPath();
            XLSTransformer transformer = new XLSTransformer();
            //向模板中写入内容
            transformer.transformXLS(xlsTemplateFileName, beanParams,filePath);
            Thread.sleep(50);
            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
            os = response.getOutputStream();
            File file = new File(filePath);
            is = new FileInputStream(file);
            byte[] b = new byte[2048];
            int length;
            while ((length = is.read(b)) > 0) {
                os.write(b, 0, length);
            }
            toClient.flush();
            toClient.close();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeInputStream(is);
            closeOutputStream(os);
        }
    }

    /**
     * 导出文件时为Writer生成OutputStream.
     * @param fileName 文件名
     * @param response response
     * @return ""
     */
    private static OutputStream getOutputStream(String fileName,
                                                HttpServletResponse response) throws Exception {
        try {
            fileName = URLEncoder.encode(fileName, "UTF-8");
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf8");
            response.setHeader("Content-Disposition", "attachment; filename=" + fileName + ".xls");
            response.setHeader("Pragma", "public");
            response.setHeader("Cache-Control", "no-store");
            response.addHeader("Cache-Control", "max-age=0");
            return response.getOutputStream();
        } catch (IOException e) {
            throw new Exception("导出excel表格失败!", e);
        }
    }

    private static void closeInputStream(InputStream is) {
        try {
            if (is != null) {
                is.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void closeOutputStream(OutputStream os) {
        try {
            if (os != null) {
                os.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
